import sys, os
from Ui_main import *
from PyQt4 import *
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4.Qwt5.qplt import *
from Numeric import *
from numpy import *
from scipy import *
from pylab import *
import matplotlib
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg
from numpy.fft import fft, rfft, fftshift, fftfreq
import def_algo as algor

class Magnet(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.ui = Ui_mainWin()
        self.ui.setupUi(self)
        
        self.signalMapper = QtCore.QSignalMapper(self)
        
        
        
        self.connect(self.ui.btnK1, QtCore.SIGNAL("clicked()"),  self.signalMapper,  QtCore.SLOT("map()"))
        self.connect(self.ui.btnK2, QtCore.SIGNAL("clicked()"),  self.signalMapper,  QtCore.SLOT("map()"))
        self.connect(self.ui.btnK3, QtCore.SIGNAL("clicked()"),  self.signalMapper,  QtCore.SLOT("map()"))
        self.connect(self.ui.btnK4, QtCore.SIGNAL("clicked()"),  self.signalMapper,  QtCore.SLOT("map()"))
        
        self.signalMapper.setMapping(self.ui.btnK1, 0)
        self.Makaj(self.ui.btnK1)
        
        
        self.connect(self.ui.pushButton,  QtCore.SIGNAL("clicked()"), self.Kresli)
        
        self.connect(self.ui.tabMag, QtCore.SIGNAL("clicked()"), self.vyber)
        
        self.ui.tabMag.setColumnCount(4)
        
        self.ui.tabMag.setHorizontalHeaderItem(0, QTableWidgetItem('K1'))
        self.ui.tabMag.setHorizontalHeaderItem(1, QTableWidgetItem('K2'))
        self.ui.tabMag.setHorizontalHeaderItem(2, QTableWidgetItem('K3'))
        self.ui.tabMag.setHorizontalHeaderItem(3, QTableWidgetItem('K4'))
        
       
        
    def Makaj(self, sig):
        print sig
        if sig == 0:
            self.k1=OtvorSubor(0)
            Tabulka(self.k1)
            PlotGraph(0, self.k1)
            
        elif sig == 1:
            self.k2=OtvorSubor(1)
            Tabulka(self.k2)
            PlotGraph(1, self.k2)
        elif sig == 2:
            self.k3=OtvorSubor(2)
            Tabulka(self.k3)
            PlotGraph(2, self.k3)
        elif sig == 3:
            self.k4=OtvorSubor(3)
            Tabulka(self.k4)
            PlotGraph(3, self.k4)
        
         
        #self.canvas = FigureCanvasQTAgg(self.ui.plotWidget)
        self.TempAdres = '/home'
        self.axes = self.ui.plotWidget
        
    def OtvorSubor(self,  k):
        if k == 0:
            line = self.ui.lineK1
        elif k == 1:
            line = self.ui.lineK2
        elif k == 2:
            line = self.ui.lineK3
        elif k == 3:
            line = self.ui.lineK4
            
        subor = QtGui.QFileDialog.getOpenFileName(self, 'Choose a file to open', self.TempAdres, 'Text Files *.txt')
        if subor:
            self.TempAdres = os.path.dirname(str(subor))
            line.setText(subor)
            f = open(subor, 'r')
            k = loadtxt(f)
        f.close()
        return k
        
        
    def Tabulka(self, k):
        row = 0
        for line in k:
            if self.ui.tabMag.rowCount() < row:
                self.ui.tabMag.insertRow(row)
            elif self.ui.tabMag.rowCount() == row:
                self.ui.tabMag.insertRow(row)
            self.ui.tabMag.setItem(row, k, QTableWidgetItem(str(line)))
            row = row + 1
           
           
           
    def PlotGraph(self,  k,  data):
        if k == 0:
            graph = self.ui.qPlot
            graph_fft = self.ui.qPlot_fft
            popis = "K1"
        elif k == 1:
            graph = self.ui.qPlot_2
            graph_fft = self.ui.qPlot_fft_2
            popis = "K2"
        elif k == 2:
            graph = self.ui.qPlot_3
            graph_fft = self.ui.qPlot_fft_3 
            popis = "K3"
        elif k == 3:
            graph = self.ui.qPlot_4
            graph_fft = self.ui.qPlot_fft_4
            popis = "K4"
        
            
        self.grid.attach(graph)
        
        self.curve = QwtPlotCurve(popis)
        self.curve2 = QwtPlotCurve("FFT")
                
        ff = rfft(data)
        
        self.curve.setPen(QPen(Qt.red))
        self.curve2.setPen(QPen(Qt.blue))
        r = arange(0,  len(data), 1)
        r2 = arange(0,  len(ff), 1)
            
        self.curve.setData(r, data)
        self.curve2.setData(r2, abs(ff[1:len(ff)]))
           
        graph.insertLegend(Qwt.QwtLegend(), Qwt.QwtPlot.BottomLegend)
        
        self.curve.attach(graph)
        self.curve2.attach(graph_fft)
        
        graph.replot()   
        graph_fft.replot()

    
    def Kresli(self):
        B = algor.Algo(10, 10, self.k1, self.k2, self.k3, self.k4)
        dat = B.Vypocet()
        for i in range(0, 10):
            obr = resize(dat[:,i], (10, 10))
            im = imshow(obr,  interpolation='bicubic', animated=True)
            im.set_data(obr)
        show()
    
    def vyber(self):
        list = self.ui.tabMag.selectedItems()
        print list
        print 'ok'
